﻿package com.CrmDemo.services;

import java.io.Serializable;

import com.CrmDemo.services.*;
import com.CrmDemo.util.*;

import com.CrmDemo.entity.CstService;

/**
 * 客户服务 管理业务逻辑类
 * @author hailong.liu
 */
public class Service extends BaseService {
	
	/**
	 * 加载 客户服务
	 */
	public CstService get(Serializable id){
		CstService ret = (CstService)this.getCommonDAO().get(CstService.class, id);		
		return ret;
	}
	
	/**
	 * 添加 客户服务
	 */
	public boolean add(CstService item){
		item.setSvrCreateDate(new java.sql.Date(new java.util.Date().getTime()));
		this.getCommonDAO().add(item);		
		return true;
	}
	
	/**
	 * 删除 客户服务
	 */
	public boolean del(Serializable id){
		this.getCommonDAO().del(CstService.class, id);		
		return true;
	}
	
	/**
	 * 修改 客户服务
	 */
	public boolean update(CstService item){
		this.getCommonDAO().update(item);		
		return true;
	}
	
	/**
	 * 查询 客户服务 
	 */
	public void search(CstService condition, PageResult pageResult) {
		String hql = "select o from CstService o where 1=1 ";
		if (null!=condition){				
				if (isNotNullOrEmpty(condition.getSvrType())){
				hql += "and o.svrType like '%"
						+condition.getSvrType()+"%' ";
			}

				if (isNotNullOrEmpty(condition.getSvrTitle())){
				hql += "and o.svrTitle like '%"
						+condition.getSvrTitle()+"%' ";
			}

			
				if (isNotNullOrEmpty(condition.getSvrCustName())){
				hql += "and o.svrCustName like '%"
						+condition.getSvrCustName()+"%' ";
			}

				if (isNotNullOrEmpty(condition.getSvrStatus())){
				hql += "and o.svrStatus like '%"
						+condition.getSvrStatus()+"%' ";
			}

				if (isNotNullOrEmpty(condition.getSvrRequest())){
				hql += "and o.svrRequest like '%"
						+condition.getSvrRequest()+"%' ";
			}

			
				if (isNotNullOrEmpty(condition.getSvrCreateBy())){
				hql += "and o.svrCreateBy like '%"
						+condition.getSvrCreateBy()+"%' ";
			}
				if (isNotNullOrEmpty(condition.getSvrDeal())){
				hql += "and o.svrDeal like '%"
						+condition.getSvrDeal()+"%' ";
			}

			
				if (isNotNullOrEmpty(condition.getSvrDealBy())){
				hql += "and o.svrDealBy like '%"
						+condition.getSvrDealBy()+"%' ";
			}

			
				if (isNotNullOrEmpty(condition.getSvrResult())){
				hql += "and o.svrResult like '%"
						+condition.getSvrResult()+"%' ";
			}

			
		}
		if (isNotNullOrEmpty(pageResult.getOrderBy())){
			String sort = pageResult.getSort();
			hql += "order by " + pageResult.getOrderBy() + " " +sort; 
			if ("asc".equals(sort)){
				pageResult.setSort("desc");
			}else{
				pageResult.setSort("asc");
			}
		}else{
			hql += "order by o.svrId desc";
		}		
		this.getCommonDAO().listByPage(hql,pageResult);
	}

	public void dispatch(long svrId, long dueId, String dueTo) {
		CstService item = this.get(svrId);
		item.setSvrDueId(dueId);
		item.setSvrDueTo(dueTo);
		item.setSvrDueDate(new java.sql.Date(new java.util.Date().getTime()));
		item.setSvrStatus("已分配");
		this.getCommonDAO().update(item);
	}

	public void deal(CstService svr) {
		Long svrId = svr.getSvrId();
		CstService item = this.get(svrId);
		item.setSvrDeal(svr.getSvrDeal());
		item.setSvrDealId(svr.getSvrDealId());
		item.setSvrDealBy(svr.getSvrDealBy());
		item.setSvrDealDate(new java.sql.Date(new java.util.Date().getTime()));
		item.setSvrStatus("已处理");
		this.getCommonDAO().update(item);
	}

	public void feedback(CstService svr) {
		Long svrId = svr.getSvrId();
		CstService item = this.get(svrId);
		item.setSvrResult(svr.getSvrResult());
		item.setSvrSatisfy(svr.getSvrSatisfy());
		//客户满意度不到3，则打回重新处理
		if (svr.getSvrSatisfy()<3){
			item.setSvrStatus("已分配");
		}else{
			item.setSvrStatus("已归档");
		}
		this.getCommonDAO().update(item);
	}
}
